//
//  TaskCell.swift
//  VenusTodo
//
//  Created by 文显庆 on 2021/10/21.
//

//给每个任务分配一个Cell

import SwiftUI

struct TaskCell: View {
    let Task: ThingsTodo
    var body: some View {
        HStack {
        Image(systemName: "circle.fill").foregroundColor(colors[Task.colorIndex])
            VStack(alignment: .leading, spacing: 5) {
                if !Task.isDone {
                    Text(Task.name)
                    .font(.caption)
                    .fontWeight(.bold)
                } else {
                    Text(Task.name)
                    .font(.caption)
                    .fontWeight(.bold)
                        .strikethrough()
                }
                
                Text(Task.additional)
                    .font(.caption)
                    .foregroundColor(.secondary)
            }
            Spacer()
            if Task.taskIndex == 0 {
                ZStack {
                    Circle()
                        .stroke(lineWidth: 5.0)
                        .opacity(0.3)
                        .foregroundColor(colors[Task.colorIndex])
                    Circle()
                        .trim(from: /*@START_MENU_TOKEN@*/0.0/*@END_MENU_TOKEN@*/, to: CGFloat(min(Double(Double(Task.progress) / Double(Task.target)), 1.0)))
                        .stroke(style: StrokeStyle(lineWidth: 5.0, lineCap: .round, lineJoin: .round))
                        .foregroundColor(colors[Task.colorIndex])
                        .rotationEffect(Angle(degrees: 270.0))
                        .animation(.linear)
                    VStack{
                        Text("当前进度")
                            .font(.system(size: 5))
                            .bold()
                        Text(String(format: "%.0f %%", min(Double(Double(Task.progress) / Double(Task.target)), 1.0) * 100.0))
                            .font(.system(size: 10))
                            .bold()
                    }
                    
                }
                Spacer()
            } else {
                if(Task.isDone == true) {
                    VStack {
                        Text("已完成").font(.caption2)
                        Image(systemName: "checkmark")
                            .foregroundColor(Color.green)
                    }
                }else {
                    VStack {
                        Text("未完成").font(.caption2)
                        Image(systemName: "slowmo")
                            .foregroundColor(Color.red)
                    }
                }
                Spacer()
            }
            
        VStack(alignment: .trailing, spacing: 5) {
            Text("开始：\(Task.formatedDateStart)")
                .font(.caption2)
                .foregroundColor(.black)
            Text("结束：\(Task.formatedDateEnd)")
                .font(.caption2)
                .foregroundColor(.black)
        }
        
    }
    }//body
    
    
}//struct


#if DEBUG
struct TaskCell_Previews: PreviewProvider {
    static var previews: some View {
        TaskCell(Task: thingDefault)
    }
}
#endif
